<?php
namespace app\api\model;
use think\Model;
use think\Db;
class  Userinfo extends Model{
    protected $autoWriteTimestamp = false;
    protected $table = 'gx_cuser_info';
    public function selectUserInfo($user_id){
       $res=Db::name('cuser_info')->where('uid',$user_id)->find();
       if(!empty($res)){
           return $res;
       }else{
           return 0;
       }
    }
    public function add($data){
        try {
           $id=$this->allowField(true)->save($data);
           if($id){
               return 1;
           }else{
               return 0;
           }
        }catch (\think\Exception $e){
            trace('inser exception'.$e->getMessage());
        }
    }
    public function edit($data){
        try {
            $id=$this->update($data);
            if($id){
                return 1;
            }else{
                return 0;
            }
        }catch (\think\Exception $e){
            trace('edit failed'.$e->getMessage());
        }
    }
    /**
     * 修改密码
     * 1.通过旧密码修改密码
     * 2.通过手机验证修改密码
     */
    public function editPassword($where){
        try {
           $res=$this->checkOldPwd($where['user_id'],$where['oldpassword']);
           if(empty($res)){
               ajaxReturn2(0,'oldpassword is error','');
           }
          if($where['oldpassword']==$where['newpassword']){
              ajaxReturn2(0,'old new is same','');
          }
           
          $id=Db::name('cuser')->where('id',$where['user_id'])->update(array('password'=>md5($where['newpassword'])));
          if($id){
              ajaxReturn2(1,'edit sucess','');
          }else{
              ajaxReturn2(0,'edit failed','');
          }
        }catch (\think\Exception $e){
            trace('edit failed'.$e->getMessage());
        }
    }
    //验证旧密码
    public function checkOldPwd($user_id,$oldpassword){
       $res= Db::name('cuser')->field('password')->where(array('id'=>$user_id,'password'=>md5($oldpassword)))->find();
       if(!empty($res)){
           return $res;
       }else{
           return 0;
       }
    }
    //2.同过手机验证码修改密码
    public function editPasswordByCode($post){
        try {
          $res=Db::name('cuser')->where(array('telphone'=>$post['telphone'],'id'=>$post['uid']))->update(array('password'=>md5($post['password'])));
          if($res){
              return 1;
          }else {
              return 0;
          }
        }catch (\think\Exception $e){
            trace('edit password '.$e->getMessage());
        }
    }
    //修改绑定的手机号
    public function editTelphone($post){
        //
        try {
           $res=Db::name('cuser')->where(array('id'=>$post['uid']))->update(array('telphone'=>$post['newtelphone']));
           if($res){
               return 1;
           }else {
               return 0;
           }
        }catch (\think\Exception $e){
            trace('editphone failed'.$e->getMessage());
        }
    }
    /**
     * 
     */
    public function selectItem($uid){
        try {
            $res=Db::name('cuser_info')->where(array('uid'=>$uid))
            ->find();
            return $res;
        }catch (\think\Exception $e){
            trace('unkown exception'.$e->getMessage());
        }
    }
    public function editinfo($id,$data){
        $res=$this->where('uid',$id)
        ->update($data);
        if($res){
            return $res;
        }else{
            return 0;
        }
    }
    public function selectoldpwd($uid,$oldpwd){
        $res=Db::name('cuser')->field('password')
        ->where(array('id'=>$uid,'password'=>md5($oldpwd)))
        ->find();
        if(!empty($res)){
            return $res;
        }else{
            return 0;
        }
    }
    public function checkpwd($oldpwd,$newpwd){
        if($oldpwd!=$newpwd){
            return 0;
        }else{
            return 1;
        }
    }
    public function updatepwd($uid,$data){
       $res=db('cuser')->where('id',$uid)->setField('password',md5($data['newpwd']));
       if($res){
           return 1;
       }else{
           return 0;
       }
    }
    public function selectphone($uid){
       $res= Db::name('cuser')->field('telphone')
        ->where('id',$uid)
        ->find();
       return $res;
    }
    public function checkphone($uid,$telphone){
       $res= Db::name('cuser')->field('id')
        ->where(array('id'=>$uid,'telphone'=>$telphone))
        ->find();
       if(!empty($res)){
           return 1;
       }else{
           return 0;
       }
    }
    public function updatephone($uid,$telphone){
      $res= db('cuser')->where('id',$uid)->setField('telphone',$telphone);
        if($res){
            return 1;
        }else {
            return 0;
        }
    }
}